module executor.server.msgServer.InitExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import std.array;
import std.conv : to;
import hunt.util.Serialize;
import client.GateServerClient;
import client.MsgServerClient;
import service.FriendService;

/**
程序启动初始化模块
**/

@TransportServer(LogicServerName)
class InitExecutor : AbstractExecutor!(InitExecutor){

  //msgserver 加载chatMap列表接口
  @MessageId(MESSAGE.CMD_INITCHATMAP)
  void initChatMap(TransportContext ctx, MessageBuffer buffer) {
    auto req = new InitChatMap();
    buffer.data.fromProtobuf!InitChatMap(req);
    logInfo("ChatExecutor ------ %d ---- %d before ",req.index,req.size);
    ChatMap mp = FriendService.processChatMap(req.index,req.size);
    logInfo("ChatExecutor ------ %d ---- %d  after ",req.index,req.size);
    ctx.session().send(new MessageBuffer(MESSAGE.CMD_CHATMAP,mp.toProtobuf.array)); //初始化动作 向msgserver同步hash到的chatId以及成员列表
  }

  //gateServer 注册连接接口
  @MessageId(MESSAGE.CMD_REGISTER_GATESERVER)
  void getGateServerConnection(TransportContext ctx, MessageBuffer buffer) {
    logInfo("CMD_REGISTER_GATESERVER ... %s",ctx.id );
    //TransportSession[ulong] clients = GateServerClient.getInstance.getForwardClient();
    //if (ctx.id in clients)
    //{
    //  logWarning("gateclient %d is exist" , ctx.id);
    //}else
    //{
      GateServerClient.getInstance.addForwardClient(ctx,ctx.id);//初始化动作，保存来自gateserver的连接
   // }
  }

  //msgerver 注册连接接口
  @MessageId(MESSAGE.CMD_REGISTER_MSGSERVER)
  void getMsgServerConnection(TransportContext ctx, MessageBuffer buffer) {
    int id = to!int (cast(string)(buffer.data));
    MsgServerClient.getInstance().addForwardClient(cast(uint)id, ctx); //初始化动作，保存来自msgserver的连接
  }

}
